﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.SessionState;

namespace Qzone.OAuth
{
    public class QzoneOAuthCallBackHandler : IHttpHandler, IRequiresSessionState
    {
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        public void ProcessRequest(HttpContext context)
        {
            string type = context.Request["type"];

            if (type == "loginbutton")
            {
                #region 用按钮控件登录
                string appid = System.Configuration.ConfigurationManager.AppSettings["Qzone.OAuth.AppId"];
                string appkey = System.Configuration.ConfigurationManager.AppSettings["Qzone.OAuth.AppKey"];
                string callBackUrl = System.Configuration.ConfigurationManager.AppSettings["Qzone.OAuth.CallBackUrl"];

                if (string.IsNullOrEmpty(callBackUrl))
                {
                    callBackUrl = context.Request.Url.Authority + "/qzoneoauth.axd";
                }

                string btnid = context.Request["btnid"];
                string ispostback = context.Request["ispostback"];
                string clientfun = context.Request["clientfun"];

                if (string.IsNullOrEmpty(appid))
                {
                    context.Response.Write("请在网站Web.Config的configuration/appSettings节点添加配置节：<br/> &lt;add key=\"Qzone.OAuth.AppId\" value=\"\"/&gt;<br/> value 值是申请QQ登录成功后，分配给网站的app id。");
                    return;
                }

                if (string.IsNullOrEmpty(appkey))
                {
                    context.Response.Write("请在网站Web.Config的configuration/appSettings节点添加配置节：<br/>  &lt;add key=\"Qzone.OAuth.AppKey\" value=\"\"/&gt;<br/> value 值是申请QQ登录成功后，分配给网站的app key。");
                    return;
                }

                OAuthHelper.Login(appid, appkey, callBackUrl,
                   string.Format("qzoneoauth.axd?type=loginsuccess&btnid={0}&ispostback={1}&clientfun={2}", btnid, ispostback, clientfun), "qzoneoauth.axd?type=loginfail");
                return;
                #endregion
            }
            else if (type == "loginsuccess")
            {
                #region 成功
                string html = @"<!DOCTYPE html PUBLIC ""-//W3C//DTD XHTML 1.0 Transitional//EN"" ""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"">
<html xmlns=""http://www.w3.org/1999/xhtml"">
<head>
    <title></title>
    <script type=""text/javascript"">
        function closeWindow() {
            if (window.opener != null) {
                window.opener.Close_Qzone_OAuth_Window(""" + context.Request["btnid"] + @""",""" + context.Request["clientfun"] + @""",""" + context.Request["ispostback"] + @""");
                window.close();
            }
        }

        closeWindow();
    </script>
</head>
<body>
</body>
</html>";
                context.Response.Write(html);
                return;
                #endregion
            }
            else if (type == "loginfail")
            {
                #region 失败
                string html = @"<!DOCTYPE html PUBLIC ""-//W3C//DTD XHTML 1.0 Transitional//EN"" ""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"">
<html xmlns=""http://www.w3.org/1999/xhtml"">
<head>
    <title></title>
</head>
<body>
error_code=" + context.Request["error_code"] + @"
</body>
</html>";
                context.Response.Write(html);
                return;
                #endregion
            }
            else
            {
                #region 腾讯调转
                string oauth_token = context.Request["oauth_token"];
                string oauth_vericode = context.Request["oauth_vericode"];

                if (!string.IsNullOrEmpty(oauth_token) && !string.IsNullOrEmpty(oauth_vericode))
                {
                    OAuthHelper.RequestAccessToken(OAuthHelper.AppId, OAuthHelper.AppKey, oauth_token, OAuthHelper.OAuth_Token_Secret, oauth_vericode);
                    return;
                }
                #endregion
            }

            context.Response.Write("找不到操作类型。");
        }
    }
}
